package maze;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.LinkedList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import Solver.Node;

/**
 * A graphical representation for the output of
 * the searching problem
 *
 */

public class MazeGUI extends JFrame implements MouseListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	Maze maze;
	static Color[] colors = {new Color(160, 82, 45), new Color(205, 104, 57), new Color(188, 118, 66),
			new Color(166, 128, 100), new Color(219, 147, 112), new Color(251, 161, 108), 
			new Color(228, 120, 51), new Color(255, 119, 34), new Color(238, 121, 66), new Color(244, 164, 96),
			new Color(255, 153, 85), new Color(205, 102, 29), new Color(210, 105, 30), 
			new Color(188, 118, 66)};
		
	static LinkedList<Node> l;
	static int cur;
	private JButton next;
	private JButton prev;
	private JButton reset;
	private JLabel search;
	private JLabel pathCost;
	private JLabel move;
	private int moves;
	
	public MazeGUI(LinkedList<Node> list, String ss, int pc) {
		super("Mouse in a maze");
		this.setLayout(null);
		this.setBounds(150, 50, 700, 550);
		l = list;
		cur = 0;
		moves = 0;
		next = new JButton("Next >>");
		next.setBounds(290, 350, 110, 30);
		next.addMouseListener(this);
		prev = new JButton("<< Previous");
		prev.setBounds(150, 350, 110, 30);
		prev.addMouseListener(this);
		reset = new JButton("Reset");
		reset.setBounds(430, 350, 110, 30);
		reset.addMouseListener(this);
		search = new JLabel("Search using: " + ss);
		search.setBounds(400, 70, 150, 50);
		search.setFont(new Font("SanSerif", Font.PLAIN, 16));
		pathCost = new JLabel("Path Cost: " + pc);
		pathCost.setBounds(400, 120, 150, 50);
		pathCost.setFont(new Font("SanSerif", Font.PLAIN, 16));
		nextMaze();
		this.setBackground(new Color(218, 244, 240));
		this.validate();
		this.repaint();
		JFrame.setDefaultLookAndFeelDecorated(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);					
		this.setVisible(true);
	}
	
	public void nextMaze() {
		Node n = l.get(cur);
		Maze m = n.getMaze();
		JPanel p = new JPanel();
		p.setLayout(null);
		p.setBackground(new Color(218, 244, 240));
		for (int i = 0; i < m.getBoard().length; i++) {
			for (int j = 0; j < m.getBoard()[i].length; j++) {
				JButton b = new JButton("");
				if (m.getGoal().getX() == i && m.getGoal().getY() == j){
					b.setBackground(new Color(72, 130, 20));
					b.setBorder(null);
				} else if (m.getBoard()[i][j] == 0) {
					b.setBackground(new Color(196, 196, 196));
					b.setBorder(null);
					//b.setContentAreaFilled(false);
				} else if (m.isMouseID(m.getBoard()[i][j])){
					b.setBackground(Color.white);
					b.setBorder(null);
					b.setText(m.getBoard()[i][j] + "");					
				} else {
					b.setBackground(colors[m.getBoard()[i][j] - 1]);
					//b.setBackground(new Color(135, 66, 31));
					b.setText(m.getBoard()[i][j] + "");					
					b.setBorder(null);					
				}
				b.setEnabled(false);
				b.setForeground(new Color(0, 0, 0));
				b.setBounds(j*50, i*50, 50, 50);
				p.add(b);
			}						
		}		
		move = new JLabel("Original Maze");
		if (moves!= 0)
			move = new JLabel(moves + ": " + n.getOperator());
		move.setBounds(400, 170, 150, 50);
		move.setFont(new Font("SanSerif", Font.PLAIN, 16));
		p.setBounds(200, 100, 500, 500);
		p.add(next);
		p.add(prev);
		p.add(reset);		
		p.add(search);
		p.add(pathCost);
		p.add(move);
		this.setContentPane(p);
		this.validate();
		this.repaint();
	}
	
	public static void main(String[] args) {
		Maze m = new Maze();
		Maze m2 = new Maze();
		LinkedList<Maze> li = new LinkedList<Maze>();
		li.add(m);
		li.add(m2);
		//new MazeGUI(li);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (e.getSource() == next) {
			if (cur < l.size()-1) {
				cur++;				
				moves++;
			}
			nextMaze();								
		}	
		if (e.getSource() == prev) {
			if (cur > 0) {
				cur--;
				moves--;
			}
			nextMaze();
		}
		if (e.getSource() == reset) {
			cur = 0;
			moves = 0;
			nextMaze();
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
